/*
 * @(#) $Header$
 *
 * Copyright (C) 2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.crypto.md;

import ca.forklabs.crypto.md.MDFamily;

/**
 * This class embodies the extension to the algorithm for the message digest MD4
 * according to
 * <a target="external" href="http://www.faqs.org/rfcs/rfc1186.html">RFC1186</a>.
 * It is more elegant to get an instance with the factory method in class
 * {@link java.security.MessageDigest} :
 * <blockquote>
 * <code>MessageDigest md4ext = MessageDigest.getInstance(Forklabs.MD_4_Ext, Forklabs.NAME);</code>
 * </blockquote>
 * <b>NOTE</b>: There does not seem to be test vectors for MD4Ext, therefore this
 * class cannot be considered to faithfully implement the algorithm, altough it
 * appears to.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.crypto.md.MD4Ext">Daniel Léonard</a>
 * @version $Revision$
 * @see      ca.forklabs.crypto.Forklabs
 */
public class MD4Ext extends MDFamily {

//----------------------------
// Class variables
//----------------------------

   /** The name of this message digests. */
   @SuppressWarnings("nls")
   public static final String NAME = "MD4Ext";

   /** The length in bytes of the digest. */
   private static final int DIGEST_LEN = 32;

   /** The size of the internal state in block of 32 bits. */
   public static final int STATE_LEN = 8;


//----------------------------
// Constructors
//----------------------------

   /**
    * Constructor.
    */
   public MD4Ext() {
      super(MD4Ext.NAME, MD4Ext.DIGEST_LEN, MD4Ext.STATE_LEN);
      this.coreInit();
      }


//----------------------------
// MD4 scrambling methods
//----------------------------

   private static int F(int b, int c, int d) {
      return ((b & c) | ((~b) & d));
      }

   private static int G(int b, int c, int d) {
      return ((b & c) | (b & d) | (c & d));
      }

   private static int H(int b, int c, int d) {
      return (b ^ c ^ d);
      }


//----------------------------
// Concrete core methods
//----------------------------

   @Override
   protected void coreTransform() {
      int[] state = this.getState();
      int a = state[0];
      int b = state[1];
      int c = state[2];
      int d = state[3];
      int aa = state[4];
      int bb = state[5];
      int cc = state[6];
      int dd = state[7];
      int tmp = 0;

      byte[] buffer = this.getBuffer();
      int x_0  = AbstractBlockMessageDigest.makeInt(buffer, 3,  2,  1,  0);
      int x_1  = AbstractBlockMessageDigest.makeInt(buffer, 7,  6,  5,  4);
      int x_2  = AbstractBlockMessageDigest.makeInt(buffer, 11, 10, 9,  8);
      int x_3  = AbstractBlockMessageDigest.makeInt(buffer, 15, 14, 13, 12);
      int x_4  = AbstractBlockMessageDigest.makeInt(buffer, 19, 18, 17, 16);
      int x_5  = AbstractBlockMessageDigest.makeInt(buffer, 23, 22, 21, 20);
      int x_6  = AbstractBlockMessageDigest.makeInt(buffer, 27, 26, 25, 24);
      int x_7  = AbstractBlockMessageDigest.makeInt(buffer, 31, 30, 29, 28);
      int x_8  = AbstractBlockMessageDigest.makeInt(buffer, 35, 34, 33, 32);
      int x_9  = AbstractBlockMessageDigest.makeInt(buffer, 39, 38, 37, 36);
      int x_10 = AbstractBlockMessageDigest.makeInt(buffer, 43, 42, 41, 40);
      int x_11 = AbstractBlockMessageDigest.makeInt(buffer, 47, 46, 45, 44);
      int x_12 = AbstractBlockMessageDigest.makeInt(buffer, 51, 50, 49, 48);
      int x_13 = AbstractBlockMessageDigest.makeInt(buffer, 55, 54, 53, 52);
      int x_14 = AbstractBlockMessageDigest.makeInt(buffer, 59, 58, 57, 56);
      int x_15 = AbstractBlockMessageDigest.makeInt(buffer, 63, 62, 61, 60);

      AbstractBlockMessageDigest.zeroize(buffer);

   // rvalue is the expansion of the macros FF, GG, HH
   // round 1
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.F(b, c, d) + x_0 ,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.F(a, b, c) + x_1 ,  7);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.F(d, a, b) + x_2 , 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.F(c, d, a) + x_3 , 19);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.F(b, c, d) + x_4 ,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.F(a, b, c) + x_5 ,  7);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.F(d, a, b) + x_6 , 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.F(c, d, a) + x_7 , 19);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.F(b, c, d) + x_8 ,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.F(a, b, c) + x_9 ,  7);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.F(d, a, b) + x_10, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.F(c, d, a) + x_11, 19);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.F(b, c, d) + x_12,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.F(a, b, c) + x_13,  7);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.F(d, a, b) + x_14, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.F(c, d, a) + x_15, 19);
   // parallel round 1
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.F(bb, cc, dd) + x_0 ,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.F(aa, bb, cc) + x_1 ,  7);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.F(dd, aa, bb) + x_2 , 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.F(cc, dd, aa) + x_3 , 19);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.F(bb, cc, dd) + x_4 ,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.F(aa, bb, cc) + x_5 ,  7);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.F(dd, aa, bb) + x_6 , 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.F(cc, dd, aa) + x_7 , 19);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.F(bb, cc, dd) + x_8 ,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.F(aa, bb, cc) + x_9 ,  7);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.F(dd, aa, bb) + x_10, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.F(cc, dd, aa) + x_11, 19);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.F(bb, cc, dd) + x_12,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.F(aa, bb, cc) + x_13,  7);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.F(dd, aa, bb) + x_14, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.F(cc, dd, aa) + x_15, 19);
   // exchange
      tmp = aa; aa = a; a = tmp;
   // round 2
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.G(b, c, d) + x_0  + 0x5a827999,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.G(a, b, c) + x_4  + 0x5a827999,  5);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.G(d, a, b) + x_8  + 0x5a827999,  9);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.G(c, d, a) + x_12 + 0x5a827999, 13);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.G(b, c, d) + x_1  + 0x5a827999,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.G(a, b, c) + x_5  + 0x5a827999,  5);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.G(d, a, b) + x_9  + 0x5a827999,  9);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.G(c, d, a) + x_13 + 0x5a827999, 13);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.G(b, c, d) + x_2  + 0x5a827999,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.G(a, b, c) + x_6  + 0x5a827999,  5);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.G(d, a, b) + x_10 + 0x5a827999,  9);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.G(c, d, a) + x_14 + 0x5a827999, 13);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.G(b, c, d) + x_3  + 0x5a827999,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.G(a, b, c) + x_7  + 0x5a827999,  5);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.G(d, a, b) + x_11 + 0x5a827999,  9);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.G(c, d, a) + x_15 + 0x5a827999, 13);
   // parallel round 2
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.G(bb, cc, dd) + x_0  + 0x50a28be6,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.G(aa, bb, cc) + x_4  + 0x50a28be6,  5);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.G(dd, aa, bb) + x_8  + 0x50a28be6,  9);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.G(cc, dd, aa) + x_12 + 0x50a28be6, 13);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.G(bb, cc, dd) + x_1  + 0x50a28be6,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.G(aa, bb, cc) + x_5  + 0x50a28be6,  5);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.G(dd, aa, bb) + x_9  + 0x50a28be6,  9);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.G(cc, dd, aa) + x_13 + 0x50a28be6, 13);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.G(bb, cc, dd) + x_2  + 0x50a28be6,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.G(aa, bb, cc) + x_6  + 0x50a28be6,  5);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.G(dd, aa, bb) + x_10 + 0x50a28be6,  9);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.G(cc, dd, aa) + x_14 + 0x50a28be6, 13);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.G(bb, cc, dd) + x_3  + 0x50a28be6,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.G(aa, bb, cc) + x_7  + 0x50a28be6,  5);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.G(dd, aa, bb) + x_11 + 0x50a28be6,  9);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.G(cc, dd, aa) + x_15 + 0x50a28be6, 13);
   // exchange
      tmp = aa; aa = a; a = tmp;
   // round 3
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.H(b, c, d) + x_0  + 0x6ed9eba1,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.H(a, b, c) + x_8  + 0x6ed9eba1,  9);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.H(d, a, b) + x_4  + 0x6ed9eba1, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.H(c, d, a) + x_12 + 0x6ed9eba1, 15);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.H(b, c, d) + x_2  + 0x6ed9eba1,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.H(a, b, c) + x_10 + 0x6ed9eba1,  9);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.H(d, a, b) + x_6  + 0x6ed9eba1, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.H(c, d, a) + x_14 + 0x6ed9eba1, 15);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.H(b, c, d) + x_1  + 0x6ed9eba1,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.H(a, b, c) + x_9  + 0x6ed9eba1,  9);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.H(d, a, b) + x_5  + 0x6ed9eba1, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.H(c, d, a) + x_13 + 0x6ed9eba1, 15);
      a = AbstractBlockMessageDigest.rotateLeft(a + MD4Ext.H(b, c, d) + x_3  + 0x6ed9eba1,  3);
      d = AbstractBlockMessageDigest.rotateLeft(d + MD4Ext.H(a, b, c) + x_11 + 0x6ed9eba1,  9);
      c = AbstractBlockMessageDigest.rotateLeft(c + MD4Ext.H(d, a, b) + x_7  + 0x6ed9eba1, 11);
      b = AbstractBlockMessageDigest.rotateLeft(b + MD4Ext.H(c, d, a) + x_15 + 0x6ed9eba1, 15);
   // parallel round 3
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.H(bb, cc, dd) + x_0  + 0x5c4dd124,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.H(aa, bb, cc) + x_8  + 0x5c4dd124,  9);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.H(dd, aa, bb) + x_4  + 0x5c4dd124, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.H(cc, dd, aa) + x_12 + 0x5c4dd124, 15);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.H(bb, cc, dd) + x_2  + 0x5c4dd124,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.H(aa, bb, cc) + x_10 + 0x5c4dd124,  9);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.H(dd, aa, bb) + x_6  + 0x5c4dd124, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.H(cc, dd, aa) + x_14 + 0x5c4dd124, 15);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.H(bb, cc, dd) + x_1  + 0x5c4dd124,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.H(aa, bb, cc) + x_9  + 0x5c4dd124,  9);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.H(dd, aa, bb) + x_5  + 0x5c4dd124, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.H(cc, dd, aa) + x_13 + 0x5c4dd124, 15);
      aa = AbstractBlockMessageDigest.rotateLeft(aa + MD4Ext.H(bb, cc, dd) + x_3  + 0x5c4dd124,  3);
      dd = AbstractBlockMessageDigest.rotateLeft(dd + MD4Ext.H(aa, bb, cc) + x_11 + 0x5c4dd124,  9);
      cc = AbstractBlockMessageDigest.rotateLeft(cc + MD4Ext.H(dd, aa, bb) + x_7  + 0x5c4dd124, 11);
      bb = AbstractBlockMessageDigest.rotateLeft(bb + MD4Ext.H(cc, dd, aa) + x_15 + 0x5c4dd124, 15);
   // exchange
      tmp = aa; aa = a; a = tmp;

   // add to old state
      state[0] += a;
      state[1] += b;
      state[2] += c;
      state[3] += d;
      state[4] += aa;
      state[5] += bb;
      state[6] += cc;
      state[7] += dd;
      }

   @Override
   protected void coreInit() {
      int[] state = this.getState();
      state[0] = 0x67452301;
      state[1] = 0xefcdab89;
      state[2] = 0x98badcfe;
      state[3] = 0x10325476;
      state[4] = 0x33221100;
      state[5] = 0x77665544;
      state[6] = 0xbbaa9988;
      state[7] = 0xffeeddcc;
      }

   }

